<template>
  <el-table :data="store.getters['menu/getMenu']" border style="width: 100%" row-key="id" default-expand-all
    :tree-props="{ children: 'children' }">
    <el-table-column prop="id" label="菜单编号" />
    <el-table-column prop="title" label="菜单名称" />
    <el-table-column prop="pid" label="上级菜单" />
    <el-table-column prop="icon" label="菜单图标" >
    <template v-slot="item">
      <div>
        <el-icon v-if="item.row.icon">
          <component :is="item.row.icon"></component>
        </el-icon>
      </div>
    </template>
    </el-table-column>
    <el-table-column prop="url" label="菜单地址" />
    <el-table-column label="状态">
      <template v-slot="item">
        <el-tag type="success" plain v-if="item.row.status == 1" size="large">启用</el-tag>
        <el-tag type="warning" size="large" plain v-else>禁用</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="200px">
      <template v-slot="item">
        <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
        <el-button type="danger" @click="del(item.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
<script lang="ts" setup>
import { useStore } from "vuex"
import { menudelete } from "../../request/api"
import { ElMessage, ElMessageBox } from 'element-plus'
import { defineEmits } from "vue"
let emit = defineEmits(["edit"])

interface Tree {
  label: string
  children?: Tree[]
}

let store = useStore()
const getMenu = () => {
  store.dispatch("menu/getMenuAction")
}
getMenu()

//删除
const del = (id: number) => {
  ElMessageBox.confirm(
    '删除后不可恢复，是否确认删除？',
    {
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      menudelete({ id }).then(res => {
        if (res.code == 200) {
          getMenu()
          ElMessage({
            type: 'success',
            message: '删除成功！',
          })
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除！',
      })
    })
}
//编辑
const edit = (id: number) => {
  emit("edit", id)

}


</script>